package CollectionBaseTest;

import java.util.HashSet;
import java.util.Iterator;

/**
 * Created by yexuan on 2018/2/26.
 */

public class HashSetDemo {
    public static void main(String[] args) {
        HashSet h = new HashSet();
        h.add(new Person("a1",11));
        h.add(new Person("a2",12));
        h.add(new Person("a1",11));
        h.add(new Person("a3",13));
        Iterator it = h.iterator();
        while (it.hasNext()){
            Person p = (Person) it.next();
            out(p.getName() +"---"+p.hashCode());
        }

//        Iterator it = h.iterator();
//        while (it.hasNext()){
//            Object obj = it.next();
//            out(obj +"---"+obj.hashCode());
//        }
    }

    public static void out(Object o){
        System.out.println(o);
    }
}

class Person{
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int hashCode(){
        return name.hashCode()+21*age;
    }

    @Override
    public boolean equals(Object obj) {
        if(!(obj instanceof Person)){
            return false;
        }
        System.out.println(((Person) obj).getName());
        return this.name.equals(((Person) obj).getName())&&this.age==((Person) obj).getAge();
    }
}